package com.example.qxfw.backgroundManagement.controller;

import com.example.qxfw.backgroundManagement.service.ProductManagerService;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.UserProduct;
import com.example.qxfw.common.entity.UserTable;
import com.example.qxfw.rcyw.pojo.JobMake;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/6/30  星期五   15:47
 * @description : 此类用来  产品管理
 ***************************************/
@Api(tags = "后台管理-产品管理")
@ApiIgnore
@RestController
@CrossOrigin
@RequestMapping("/productManager")
public class ProductManagerController {

    @Autowired
    private ProductManagerService productManagerService;

    @ApiOperation("查询user_table表的id和username")
    @PostMapping(value = "selectIdAndUserName")
    public SysResult selectIdAndUserName() {
        List<UserTable> userTables = this.productManagerService.selectIdAndUserName();
        return SysResult.success(userTables);
    }

    @ApiOperation("查询用户产品表的产品类型")
    @GetMapping(value = "selectProductType")
    public SysResult selectProductType() {
        List<UserProduct> userProducts = this.productManagerService.selectProductType();
        return SysResult.success(userProducts);
    }

    @ApiOperation("查询业务制作表的业务类型")
    @GetMapping(value = "selectJobType")
    public SysResult selectJobType(){
        List<JobMake> jobMakes = this.productManagerService.selectJobType();
        return SysResult.success(jobMakes);
    }

    @ApiOperation("从用户表中查询出所有id，再根据此id的集合以及参数查询用户产品表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "startTime",value = "时间 yyyy-MM-dd HH:mm:ss",dataType = "String",required = true),
            @ApiImplicitParam(name = "endTime",value = "时间 yyyy-MM-dd HH:mm:ss",dataType = "String",required = true),
            @ApiImplicitParam(name = "productType",value = "产品类型，selectProductType返回的类型",dataType = "String",required = true),
            @ApiImplicitParam(name = "productName",value = "产品名称，做模糊查询",dataType = "String",required = false)
    })
    @GetMapping(value = "selectUserProductByUserId")
    public SysResult selectUserProductByUserId(final String startTime, final String endTime, final String productType, final String productName){
        List<UserProduct> userProducts = this.productManagerService.selectUserProductByUserId(startTime, endTime, productType, productName);
        return SysResult.success(userProducts);
    }


    @ApiOperation("先查看最大的id,再将id+1赋予新的数据的id,添加产品信息")
    @GetMapping(value = "insertUserProduct")
    public SysResult insertUserProduct(final UserProduct userProduct){
        Integer insertUserProduct = this.productManagerService.insertUserProduct(userProduct);
        return returnInformation("添加产品信息", insertUserProduct);
    }

    @ApiOperation("更新产品信息")
    @GetMapping(value = "updateUserProduct")
    public SysResult updateUserProduct( UserProduct userProduct){
        Integer updateUserProduct = this.productManagerService.updateUserProduct(userProduct);
        return returnInformation("更新产品信息", updateUserProduct);
    }

    @ApiOperation("删除产品信息")
    @GetMapping(value = "deleteUserProduct")
    public SysResult deleteUserProduct(int id){
        Integer deleteUserProduct = this.productManagerService.deleteUserProduct(id);
        return returnInformation("删除产品信息", deleteUserProduct);
    }

    @ApiOperation("导出用户信息")
    @GetMapping(value = "exportUserTable")
    public SysResult exportUserTable(){
        List<UserProduct> userProducts = this.productManagerService.exportUserTable();
        return SysResult.success(userProducts);
    }

    @ApiOperation("导出产品信息")
    @GetMapping(value = "importUserProduct")
    public SysResult importUserProduct(final UserProduct userProduct){
        Integer importUserProduct = this.productManagerService.importUserProduct(userProduct);
        return returnInformation("导入用户产品信息", importUserProduct);
    }


    @ApiOperation("根据产品类型查询产品名")
    @GetMapping(value = "selectProductName")
    public SysResult selectProductName(final String productType){
        List<UserProduct> userProducts = this.productManagerService.selectProductName(productType);
        return SysResult.success(userProducts);
    }

    @ApiOperation("根据业务类型查询业务名称")
    @GetMapping(value = "selectJobName")
    public SysResult selectJobName(final String jobType){
        List<JobMake> jobMakes = this.productManagerService.selectJobName(jobType);
        return SysResult.success(jobMakes);
    }

    public SysResult returnInformation(String info, Integer result) {
        if (result < 0) {
            return SysResult.fail(info + "失败");
        } else {
            return SysResult.success(result);
        }
    }



}
